package datasource;

import JDBC.domain.User;
import datasource.config.c3p0Config;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by Administrator on 2017/9/14.
 */
public class UserDao {
    public int pageSize = 3;
    public List<User> findPage(int page) throws SQLException {
        Connection conn = c3p0Config.getConnection();
        List<User> list = new ArrayList<>();
        String sql = "SELECT * FROM USER ORDER BY id DESC LIMIT ? , ?;\n";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,(page-1)*pageSize);
        ps.setInt(2,pageSize);
        ResultSet set = ps.executeQuery();
        while (set.next()){
            User user = new User();
            user.setId(set.getInt(1));
            user.setUsername(set.getString(2));
            user.setPassword(set.getString(3));
            user.setEmail(set.getString(4));
            user.setPhone(set.getInt(5));
            list.add(user);
        }
        return list;
    }
}
